//
//  OneTabButView.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/18.
//

#import "OneTabButView.h"
@interface OneTabButView()

@end


@implementation OneTabButView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUI];
    }
    return self;
}

-(void)setUI
{
    
    self.tabbut = [[UIButton alloc]initWithFrame:CGRectMake(16, 10, APPwidth-32, 45)];
    self.tabbut.layer.cornerRadius = 45/2;
    self.tabbut.layer.masksToBounds = YES;
    self.tabbut.backgroundColor = rgba(22, 120, 255, 1);
    self.tabbut.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.tabbut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.tabbut addTarget:self action:@selector(tabbutCheng:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.tabbut];
    
    
    
    self.textView = [[UITextView alloc]initWithFrame:CGRectMake((APPwidth-[LongHelper textWidth:@"我已阅读并同意《用户协议》及《隐私政策》" WithUIFont:[UIFont systemFontOfSize:10] AndHeight:20]-30)/2+25, CGRectGetMaxY(self.tabbut.frame)+18, [LongHelper textWidth:@"我已阅读并同意《用户协议》及《隐私政策》" WithUIFont:[UIFont systemFontOfSize:10] AndHeight:20]+10, 20)];
    self.textView.backgroundColor = [UIColor clearColor];
    self.textView.editable = NO;
    self.textView.scrollEnabled = NO;
    [self addSubview:self.textView];

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"我已阅读并同意《用户协议》及《隐私政策》" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:rgba(51, 51, 51, 1)}];
//    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor getColor:@"12C482"]} range:NSMakeRange(7, 6)];
//    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor getColor:@"12C482"]} range:NSMakeRange(14, 6)];
    [string addAttribute:NSLinkAttributeName value:@"yonghuxieyi://" range:NSMakeRange(7,6)];
    [string addAttribute:NSLinkAttributeName value:@"yisizhengce://" range:NSMakeRange(14,6)];
    self.textView.attributedText= string;
    // 设置被点击字体颜色
    self.textView.linkTextAttributes = @{NSForegroundColorAttributeName:rgba(22, 120, 255, 1)};
    
    // 阅读协议
    self.agreebut = [[UIButton alloc]initWithFrame:CGRectMake((APPwidth-[LongHelper textWidth:@"我已阅读并同意《用户协议》及《隐私政策》" WithUIFont:[UIFont systemFontOfSize:10] AndHeight:20]-30)/2, CGRectGetMaxY(self.tabbut.frame)+23, 20, 20)];
    [self.agreebut setImage:[UIImage imageNamed:@"agree_unselected"] forState:UIControlStateNormal];
    [self.agreebut setImage:[UIImage imageNamed:@"agree_selected"] forState:UIControlStateSelected];
    [self addSubview:self.agreebut];
    [self.agreebut addTarget:self action:@selector(readAction:) forControlEvents:UIControlEventTouchUpInside];

    self.textView.hidden = YES;
    self.agreebut.hidden = YES;
}

// 阅读协议
- (void)readAction:(UIButton *)button {
    button.selected = !button.selected;
}

-(void)tabbutCheng:(UIButton *)but
{
    if ([self.delegate respondsToSelector:@selector(OneTabButViewBtnSelectedDelegate:)]) {
        [self.delegate OneTabButViewBtnSelectedDelegate:0];
    }
}


@end
